//
//  SearchController.m
//  
//
//  Created by 郭明皓 on 15/11/2.
//
//

#import "SearchController.h"
#import "GMHCitysModel.h"
#import <AFNetworking.h>
#import "SearchSurroundController.h"
#import "SearchCollectionViewCell.h"
#import "DKNightVersion.h"
#import "GMH_Share.h"
@interface SearchController ()<UICollectionViewDataSource, UICollectionViewDelegate, UISearchBarDelegate>
@property (nonatomic, retain) UISearchBar *search;
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain)NSMutableArray *myArry;
@end

@implementation SearchController

- (void)dealloc
{
    [_myArry release];
    [_collectionView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.nightBackgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];

    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 36)];
    [self.view addSubview:label];
    [label release];
    label.text = @"热门城市";
    label.nightTextColor = [UIColor darkGrayColor];
    
    
    [self CollectionVIew];
    
    _search = [[UISearchBar alloc]initWithFrame:CGRectMake(20, 0, self.view.frame.size.width - 40, 40)];
    self.navigationItem.titleView = self.search;
    _search.placeholder = @"搜索目的地";
    self.search.barStyle = UIBarStyleDefault;
    [_search release];
    _search.delegate = self;
    _search.layer.cornerRadius = 10;
    _search.layer.masksToBounds = YES;
    
    [self getData];
}
- (void)getData
{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *strurl = @"http://appapi.yaochufa.com/v2/Util/GetSearchHotKeys?keyCount=12&system=android&machineCode=ffffffff-9317-02f2-ffff-fffff60c2478&channel=xiaomimarket&cityId=38&version=4.3.1";
    [manager GET:strurl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        self.myArry = dic[@"data"][@"keys"];
        
        [self.collectionView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    SearchSurroundController *search = [[SearchSurroundController alloc]init];
    [self.navigationController pushViewController:search animated:YES];
    [search release];
    search.cityName = searchBar.text;

}
- (void)CollectionVIew
{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    flow.itemSize =  CGSizeMake(self.view.frame.size.width / 4, 30);
    flow.minimumInteritemSpacing = 0;
    flow.minimumLineSpacing = 10;//行间距
    flow.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flow];
    [self.view addSubview:self.collectionView];
    [self.collectionView release];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[SearchCollectionViewCell class] forCellWithReuseIdentifier:@"collection"];
    if ([GMH_Share SingleShare].isNight == YES) {
        self.collectionView.nightBackgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];
    }
    else
    {
        self.collectionView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
    }
    
    
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return  self.myArry.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SearchCollectionViewCell *collection = [collectionView dequeueReusableCellWithReuseIdentifier:@"collection" forIndexPath:indexPath];
//    collection.backgroundColor = [UIColor brownColor];
//    GMHCitysModel *city = self.myArry[indexPath.row];
    collection.cityName.text = self.myArry[indexPath.row];
    if ([GMH_Share SingleShare].isNight == YES) {
        collection.cityName.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];
        
    }
    else
    {
        
    }
    
    return collection;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SearchSurroundController *search = [[SearchSurroundController alloc]init];
    [self.navigationController pushViewController:search animated:YES];
    [search release];
    search.cityName = self.myArry[indexPath.row];
    NSLog(@"%@", search.cityName);
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [self.collectionView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
